function PathAction(target){
    this.target = target
    this.actions = [];
    this.isRuning = false;
    
}

PathAction.prototype.removeActions = function(){

}

PathAction.prototype.addMoveTo = function(t,vec2){
    this.actions.push({
        type : 'move',
        time : t,
        vec2 : vec2
    })
}

PathAction.prototype.addCircle = function(t,center,radius,start,end,revert){
    var s = start% (2*Math.PI)
    var e = end% (2*Math.PI)
    var _d = revert
    if(_d){
        if(e >= s){
            s+=2*Math.PI
        }
    } else {
        if(e <= s){
            e+=2*Math.PI
        }
    }
    // cc.log('addCircle',arguments)
    this.actions.push({
        type : 'circle',
        time : t,
        center:center, 
        radius:radius, 
        start:s, 
        end:e, 
        revert:revert
    })
},

PathAction.prototype.addDelayTime = function(t){
    this.actions.push({
        type : 'delay',
        time : t
    })
}

PathAction.prototype.addCallBack = function(cb){
    this.actions.push({
        type:'cb',
        cb : cb
    })
}

PathAction.prototype.start = function(repeat){
    this._startTime = Date.now();
    this.isRuning = true;
    this.repeat = repeat;
    this.index = 0;
    this.runingTime = 0;
    this.nextAction();
}

PathAction.prototype.nextAction = function(){
    var action = this.actions[this.index];
    action._init = {
        x: this.target.x,
        y:this.target.y,
        r : this.target.rotation
    }
}

PathAction.prototype.step = function(dt = 0.016){
    if(!this.isRuning){
        return;
    }
    // cc.log('step:',dt);
    this.runingTime+=dt;

    var time = this.runingTime

    while(this.index < this.actions.length){
        var action = this.actions[this.index];
        if(time >= action.time || action.type == 'cb'){
            action.type == 'cb' && action.cb && action.cb();
            action.time && (time -= action.time);
            action.time && (this.runingTime -= action.time);
            this.index++;
            // cc.log('运动切换:',Date.now()-this._startTime)
            if(this.index < this.actions.length){
                this.nextAction();
            } else if(this.repeat > 1){
                this.start(this.repeat-1)
            } else {
                this.isRuning = false;
                this.actions = [];
                // cc.log('运动结束:',Date.now()-this._startTime)
            }
        } else {
            var dl = time / action.time; 
            var x,y;
            if(action.type == 'move'){
                x = action._init.x + (action.vec2.x - action._init.x)*dl;
                y = action._init.y + (action.vec2.y - action._init.y)*dl;
                
            } else if(action.type == 'circle'){
                var a = (action.end - action.start) * dl + action.start
                a = (a + 2*Math.PI)%(2*Math.PI)
                x = action.center.x + action.radius * Math.sin(a);
                y = action.center.y + action.radius * Math.cos(a);
            }
            this.target.x = x;
            this.target.y = y;
            break;
        }
    }
    
}



module.exports = PathAction